package sui.utils
{
    import flash.display.DisplayObject;

    public class NameUtil
    {
        public static function displayObjectToString(displayObject:DisplayObject):String
        {
            var result:String;

            // Start at the specified object and walk up the parent chain
            // to build up the string to return.
            try
            {
                for (var o:DisplayObject = displayObject; o != null; o = o.parent)
                {
                    // If this object is in the display tree,
                    // stop after we've prepended the topmost Application instance.
                    if (o.parent && o.stage && o.parent == o.stage)
                        break;

                    // Prefer id over name if specified.
                    var s:String = "id" in o && o["id"] ? o["id"] : o.name;

                    result = result == null ? s : s + "." + result;
                }
            }
            catch (e:SecurityError)
            {
                // Ignore error and continue with what we have. 
                // We may not have access to our parent if we are loaded into a sandbox.
            }

            return result;
        }
    }
}
